package com.example.controller;


import com.alibaba.fastjson.JSON;
import com.example.dto.*;
import com.example.entity.AirspaceNormsAndConstraints;
import com.example.entity.AirspaceNormsAndConstraintsMini;
import com.example.service.AirspaceNormsAndConstraintsService;
import com.example.util.MicroResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Optional;
@Slf4j
@RequestMapping("/admin/airspaceNormsAndConstraints")
@Controller
public class AirspaceNormsAndConstraintsController {
    @Autowired
    private AirspaceNormsAndConstraintsService airspaceNormsAndConstraintsService;
    /**
     * 功能：分页查询与关键词模糊查询空域需求信息,
     * 输入：pageNum,pageSize keyText, order
     */
    @PostMapping("/keywordSearch")
    public ResponseEntity page(@RequestBody AirspaceNormsAndConstraintsPageQueryDTO airspaceNormsAndConstraintsPageQueryDTO) {
        System.out.println("空域约束与规范分页查询: " );
        System.out.println("空域约束与规范分页查询参数:{}"+airspaceNormsAndConstraintsPageQueryDTO );
        AirspaceNormsAndConstraintsPageQueryBDTO<AirspaceNormsAndConstraintsMini> page = airspaceNormsAndConstraintsService.page(airspaceNormsAndConstraintsPageQueryDTO);
        return ResponseEntity.ok(MicroResponseUtils.success(page));
    }
    @PostMapping("/getById")
    public ResponseEntity getById(@RequestBody AirspaceNormsAndConstraintsIdQueryDTO airspaceNormsAndConstraintsIdQueryDTO ) {
        log.info("根据id查询空域需求信息");
        log.info("根据id查询空域需求信息参数:{}", airspaceNormsAndConstraintsIdQueryDTO);
        Integer id =airspaceNormsAndConstraintsIdQueryDTO.getId();
        Optional<AirspaceNormsAndConstraints> airspaceNormsAndConstraints=airspaceNormsAndConstraintsService.getById(id);
        ResponseEntity responseEntity;
        if(airspaceNormsAndConstraints.isPresent()){
            responseEntity = ResponseEntity.ok(MicroResponseUtils.success(airspaceNormsAndConstraints.get()));
        }
        else{
            responseEntity = ResponseEntity.ok(MicroResponseUtils.error("User not found"));
        }
        return responseEntity;
    }
    @PostMapping("/informationExtraction")
    public ResponseEntity informationExtraction(@RequestBody   AirspaceNormsAndConstraintsIdQueryDTO airspaceNormsAndConstraintsIdQueryDTO ) {
        log.info("根据id查询空域需求信息");
        log.info("根据id查询空域需求信息参数:{}", airspaceNormsAndConstraintsIdQueryDTO);
        Integer id =airspaceNormsAndConstraintsIdQueryDTO.getId();
        Optional<AirspaceNormsAndConstraintsInformationExtractionBDTO> airspaceNormsAndConstraintsInformationExtraction = airspaceNormsAndConstraintsService.informationExtraction(id);
        return ResponseEntity.ok(MicroResponseUtils.success(airspaceNormsAndConstraintsInformationExtraction.get()));
    }
    @PostMapping("/informationSave")
    public ResponseEntity informationSave(@RequestBody AirspaceNormsAndConstraintsInformationSaveDTO airspaceNormsAndConstraintsInformationSaveDTO) {
        log.info("根据id查询空域需求信息");
        log.info("根据id查询空域需求信息参数:{}", airspaceNormsAndConstraintsInformationSaveDTO);
        airspaceNormsAndConstraintsService.informationSave(airspaceNormsAndConstraintsInformationSaveDTO);
        Optional<AirspaceNormsAndConstraints> airspaceNormsAndConstraints = airspaceNormsAndConstraintsService.informationSave(airspaceNormsAndConstraintsInformationSaveDTO);
        ResponseEntity responseEntity;
        if (airspaceNormsAndConstraints.isPresent()) {
            responseEntity = ResponseEntity.ok(MicroResponseUtils.success(airspaceNormsAndConstraints.get()));
        } else {
            responseEntity = ResponseEntity.ok(MicroResponseUtils.error("update failed"));
        }
        return responseEntity;
    }
    @PostMapping("/PdftoImage")
    public ResponseEntity PdftoImage(@RequestBody PdfToImageDTO PdfToImageDTO) {
        log.info("根据id查询空域需求信息");
        log.info("根据id查询空域需求信息参数:{}", PdfToImageDTO);
        Pair<Boolean, Object> pair =airspaceNormsAndConstraintsService.pdfToImage(PdfToImageDTO);
        System.out.println("PDF文档转化为图片结果：" + pair.getLeft());
        if (!pair.getLeft()) {
            System.out.println("" + pair.getRight());
        } else {
            List<String> fileList = (List<String>) pair.getRight();
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            // 将 List<String> 转换为 LinkedHashMap<String, String>
            for (int i = 0; i < fileList.size(); i++) {
                map.put(Integer.toString(i + 1), fileList.get(i));
            }
            // 将 LinkedHashMap 转换为 JSON 字符串
            String jsonString = JSON.toJSONString(map);
            System.out.println(jsonString);
            String str = JSON.toJSONString(fileList);
            String join1 = StringUtils.join(map);
            System.out.println("转化的文件的内容："+join1);

            fileList.forEach(System.out::println);
        }
        return ResponseEntity.ok(MicroResponseUtils.success(pair.getRight()));
    }
}
